package cn.iocoder.an.module.mold.service.repaircategory;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.an.module.mold.dal.dataobject.account.AccountDO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.an.module.mold.controller.admin.repaircategory.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.repaircategory.RepairCategoryDO;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import cn.iocoder.an.module.mold.dal.mysql.repaircategory.RepairCategoryMapper;

import static cn.iocoder.an.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.an.module.mold.enums.ErrorCodeConstants.*;

/**
 * 模具维修分类 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class RepairCategoryServiceImpl implements RepairCategoryService {

    @Resource
    private RepairCategoryMapper repairCategoryMapper;

    @Override
    public Long createRepairCategory(RepairCategorySaveReqVO createReqVO) {
        // 插入
        RepairCategoryDO repairCategory = BeanUtils.toBean(createReqVO, RepairCategoryDO.class);
        repairCategoryMapper.insert(repairCategory);

        // 返回
        return repairCategory.getId();
    }

    @Override
    public void updateRepairCategory(RepairCategorySaveReqVO updateReqVO) {
        // 校验存在
        validateRepairCategoryExists(updateReqVO.getId());
        // 更新
        RepairCategoryDO updateObj = BeanUtils.toBean(updateReqVO, RepairCategoryDO.class);
        repairCategoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteRepairCategory(Long id) {
        // 校验存在
        validateRepairCategoryExists(id);
        // 删除
        repairCategoryMapper.deleteById(id);
    }

    @Override
        public void deleteRepairCategoryListByIds(List<Long> ids) {
        // 删除
        repairCategoryMapper.deleteByIds(ids);
        }


    private void validateRepairCategoryExists(Long id) {
        if (repairCategoryMapper.selectById(id) == null) {
            throw exception(REPAIR_CATEGORY_NOT_EXISTS);
        }
    }

    @Override
    public RepairCategoryDO getRepairCategory(Long id) {
        return repairCategoryMapper.selectById(id);
    }

    @Override
    public PageResult<RepairCategoryDO> getRepairCategoryPage(RepairCategoryPageReqVO pageReqVO) {
        return repairCategoryMapper.selectPage(pageReqVO);
    }

    @Override
    public  List<RepairCategoryDO> getCategoryAll(){
        return repairCategoryMapper.selectList();
    };

}